package com.yonyou.brigade.facade.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.facade.service.IApplyInfoService;
import com.yonyou.brigade.facade.service.ISSOService;
import com.yonyou.brigade.service.IBrigadePublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2018/9/19 10:44
 * @Description: 商旅服务单点controller
 */
@Controller
@RequestMapping("/sso")
public class YYSpaceSSOController {
    @Autowired
    @Qualifier("SSOServiceImpl")
    private ISSOService ssoService;

    @Autowired
    @Qualifier("brigadePublicServiceImpl")
    private IBrigadePublicService brigadePublicService;

    @Autowired
    @Qualifier("applyInfoServiceImpl")
    private IApplyInfoService applyInfoService;


    /**
     * @Description: 友商旅单点重定向到第三方页面
     * @param request
     * @param response
     * @Date: 2018/9/20 21:28
     */
    @RequestMapping(value = "/rtpnr", method = RequestMethod.GET)
    public void getRtpnrLoginUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String url = ssoService.getRtpnrSSOUrl(request.getParameter("code"));
       String url= brigadePublicService.loginUrl("ft9fbcrw","0001A210000000003ADC","15901062973","rtpnr","main","h5",null);
        response.sendRedirect(url);

    }

    @RequestMapping(value = "/yihao", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> getYHLoginUrl(@RequestBody JSONObject body){
        Map result = new HashMap();
        String tenant = body.getString("tenantId");
        String org = body.getString("orgPk");
        String phone = body.getString("phone");
        String channel = body.getString("channel");
        String pageType = body.getString("pageType");
        String pcOrH5 = body.getString("pcOrH5");
        String url = brigadePublicService.loginUrl(tenant,org,phone,channel,pageType,pcOrH5,"");
        result.put("success",url);
        return result;
    }

    @RequestMapping(value = "/addApply", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> getYHAddApply(@RequestBody JSONObject body) throws Exception{
        String tenant = body.getString("tenant");
        Map applyMap = new HashMap();
        List<Map> flightList = new ArrayList<>();
        List<Map> otherList = new ArrayList<>();
        Map flightEndorsementDetails = new HashMap();
        List<Map> passengers = new ArrayList<>();
        Map passengerMap1 = new HashMap<>();
        passengerMap1.put("credentialsNumber","123456");
        passengerMap1.put("credentialsType","1");
        passengerMap1.put("isEmployee","1");
        passengerMap1.put("name","ceshi");
        passengerMap1.put("phone","15765594497");

        passengerMap1.put("empID","15765594497");
        passengerMap1.put("department","测试部门");
        passengerMap1.put("departmentNO","ceshi001");

        passengers.add(passengerMap1);

        flightEndorsementDetails.put("flightWay","dc");
        flightEndorsementDetails.put("departDateBegin","2018-10-22");
        flightEndorsementDetails.put("departDateEnd","2018-10-23");
        List<String> fromCitiesList1 = new ArrayList<>();
        fromCitiesList1.add("哈尔滨");
        flightEndorsementDetails.put("fromCities",fromCitiesList1);
        List<String> toCitiesList1 = new ArrayList<>();
        toCitiesList1.add("北京");
        flightEndorsementDetails.put("toCities",toCitiesList1  );
        flightEndorsementDetails.put("passengerList",passengers);
        flightList.add(flightEndorsementDetails);

        Map otherEndorsementDetails = new HashMap();
        List<Map> passengers2 = new ArrayList<>();
        Map passengerMap2 = new HashMap<>();
        passengerMap2.put("credentialsNumber","654321");
        passengerMap2.put("credentialsType","2");
        passengerMap2.put("isEmployee","2");
        passengerMap2.put("name","ceshi2");
        passengerMap2.put("phone","15765594497");

        passengerMap2.put("empID","15765594497");
        passengerMap2.put("department","测试部门1");
        passengerMap2.put("departmentNO","ceshi002");

        passengers2.add(passengerMap2);

        otherEndorsementDetails.put("voyageType","0");
        otherEndorsementDetails.put("travelTypeName","出租车");
        otherEndorsementDetails.put("departDateBegin","2018-10-24");
        otherEndorsementDetails.put("departDateEnd","2018-10-25");
        List<String> fromCitiesList2 = new ArrayList<>();
        fromCitiesList2.add("公司");
        otherEndorsementDetails.put("fromCities",fromCitiesList2);
        List<String> toCitiesList2 = new ArrayList<>();
        toCitiesList2.add("宿舍");
        otherEndorsementDetails.put("toCities",toCitiesList2);

        otherEndorsementDetails.put("passengerList",passengers2);
        otherList.add(otherEndorsementDetails);

        applyMap.put("phone","15765594497");
        applyMap.put("flightEndorsementDetails",flightList);
        applyMap.put("otherEndorsementDetails",otherList);
        applyMap.put("handoverStaff","15765594497");
        applyMap.put("approvalNumber","11111");
        applyMap.put("matters","22222");
        applyMap.put("fromChannel","clyh");
        applyMap.put("costCenterNo","0001");
        applyMap.put("settlementEntityCode","0010");
        applyMap.put("settlementEntityName","测试");
        applyInfoService.dealApplyInfo(applyMap,tenant);
        Map result = new HashMap();
        result.put("success","success");
        return result;
    }

    @RequestMapping(value = "/openBrigade", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> getOpenBri(@RequestBody JSONObject body){
        JSONObject reqJSON = new JSONObject();
        reqJSON.put("tenantId",body.getString("tenantId"));
        reqJSON.put("tenantName",body.getString("tenantName"));
        reqJSON.put("phone",body.getString("adminPhone"));
        reqJSON.put("orgpk",body.getString("orgpk"));
        String channel = body.getString("channel");
        reqJSON.put("channel",channel);
        reqJSON.put("openType",2);
        reqJSON.put("isPreApprove",body.getBooleanValue("isPreApprove") ? 1 : 0);
        JSONObject configJSON = new JSONObject();
        if (ChannelEnum.TUNIU.getName().equals(channel)){
            configJSON.put("apiKey",body.getString("apiKey"));
            configJSON.put("secretKey",body.getString("secretKey"));
            configJSON.put("companyName",body.getString("companyName"));
        }
        reqJSON.put("config",configJSON);

        String id = brigadePublicService.openBrigade(reqJSON);

        Map result = new HashMap();
        result.put("success",id);
        return result;
    }

    @RequestMapping(value = "/pushTravelApplyOrder", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> pushTravelApplyOrder(@RequestBody JSONObject body){
        String tenant = body.getString("tenant");
        String org = body.getString("org");
        String channel = body.getString("channel");
        JSONObject data = body.getJSONObject("data");
        String id = brigadePublicService.pushTravelApplyOrder( tenant, org, channel, data);

        Map result = new HashMap();
        result.put("success",id);
        return result;
    }
}
